package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.po.MarketGoods;
import com.project2.bean.po.MarketGoodsExample;
import com.project2.bean.po.MarketTopic;
import com.project2.bean.po.MarketTopicExample;
import com.project2.bean.vo.ReadTopicInnerGoodsInfo;
import com.project2.bean.vo.ReadTopicVo;
import com.project2.mapper.MarketGoodsMapper;
import com.project2.mapper.MarketTopicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class WxTopicServiceImpl implements WxTopicService {

    @Autowired
    MarketTopicMapper topicMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public CommonData list(PageBo pageBo) {
        // start paging
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());

        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria topicExampleCriteria = topicExample.createCriteria();
        topicExampleCriteria.andDeletedEqualTo(false);

        List<MarketTopic> allTopics = topicMapper.selectByExample(topicExample);

        // TODO 如果一个专题都没有咋办？暂时不知道PageInfo是否会出问题

        PageInfo<MarketTopic> topicPageInfo = new PageInfo<>(allTopics);

        return CommonData.data(topicPageInfo);
    }

    @Override
    public ReadTopicVo detail(Integer id) {

        // 有没有可能没有这个专题？一般不会有这个问题，除非list出问题，否则这里肯定能找到
        MarketTopic currentTopic = topicMapper.selectByPrimaryKey(id);
        Integer[] goodsIds = currentTopic.getGoods();

        // 最终返回的缩略商品信息list
        ArrayList<ReadTopicInnerGoodsInfo> goodsInfoList = new ArrayList<>();

        // 找到相关的商品全信息
        if (goodsIds.length != 0) {
            MarketGoodsExample goodsExample = new MarketGoodsExample();
            MarketGoodsExample.Criteria goodsExampleCriteriacriteria = goodsExample.createCriteria();

            goodsExampleCriteriacriteria.andIdIn(Arrays.asList(goodsIds));
            goodsExampleCriteriacriteria.andDeletedEqualTo(false);
            goodsExampleCriteriacriteria.andIsOnSaleEqualTo(true);

            List<MarketGoods> relatedGoods = goodsMapper.selectByExample(goodsExample);

            // 遍历相关商品的全信息对象，封装进缩略信息对象中
            for (MarketGoods goods : relatedGoods) {
                ReadTopicInnerGoodsInfo info = new ReadTopicInnerGoodsInfo();

                info.setId(goods.getId());
                info.setName(goods.getName());
                info.setBrief(goods.getBrief());
                info.setCounterPrice(goods.getCounterPrice().doubleValue());
                info.setRetailPrice(goods.getRetailPrice().doubleValue());
                info.setIsHot(goods.getIsHot());
                info.setIsNew(goods.getIsNew());
                info.setPicUrl(goods.getPicUrl());

                goodsInfoList.add(info);
            }
        }
        // 如果没有相关信息，缩略信息list是空list，也没问题
        ReadTopicVo readTopicVo = new ReadTopicVo();
        readTopicVo.setTopic(currentTopic);
        readTopicVo.setGoodsList(goodsInfoList);

        return readTopicVo;
    }

    @Override
    public CommonData related(Integer id) {
        // 创建条件对象
        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria topicExampleCriteria = topicExample.createCriteria();
        topicExampleCriteria.andDeletedEqualTo(false);
        topicExampleCriteria.andIdNotEqualTo(id);

        List<MarketTopic> allRelatedTopics = topicMapper.selectByExample(topicExample);

        CommonData<MarketTopic> commonData = new CommonData<>();
        commonData.setList(allRelatedTopics);
        commonData.setPage(1);
        commonData.setPages(1);
        int size = allRelatedTopics.size();
        commonData.setTotal(size);
        commonData.setLimit(size);
        return commonData;
    }
}
